package org.whatever.aha.zjut.platform.service.competition;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.whatever.aha.zjut.base.constant.ErrorCode;
import org.whatever.aha.zjut.base.constant.StatusConstant;
import org.whatever.aha.zjut.base.exception.AppException;
import org.whatever.aha.zjut.platform.dto.competition.AcademyCompetitionDto;
import org.whatever.aha.zjut.platform.entity.competition.AcademyCompetition;
import org.whatever.aha.zjut.platform.mapper.competition.AcademyCompetitionMapper;
import org.whatever.aha.zjut.platform.service.project.ProjectService;
import org.whatever.aha.zjut.platform.vo.competition.AcademyCompetitionVo;
import org.whatever.aha.zjut.platform.vo.school.AcademyCompetitionRoughInfoVo;
import org.whatever.aha.zjut.platform.vo.school.DirectProvinceInfoVo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Vc
 * @version 1.0
 * @Desc
 * @date 2021/08/30 20:36
 */
@Service
@RequiredArgsConstructor
public class AcademyCompetitionService  {
    final AcademyCompetitionMapper academyCompetitionMapper;
    final ProjectService projectService;

    /**
     * 创建新的学院比赛信息
     */
    @Transactional(rollbackFor = Exception.class, propagation= Propagation.REQUIRED)
    public int addAcademyComp(int stageId, AcademyCompetitionDto academyCompetitionDto){
        AcademyCompetition academyCompetition = new AcademyCompetition();
        BeanUtils.copyProperties(academyCompetitionDto, academyCompetition);
        academyCompetition.setStageId(stageId);
        return academyCompetitionMapper.insert(academyCompetition);
    }

    /**
     * 根据List创建新的学院比赛信息
     */
    @Transactional(rollbackFor = Exception.class, propagation= Propagation.REQUIRED)
    public int addAcademyCompByList(int stageId, List<AcademyCompetitionDto> academyCompetitionDtoList){
        academyCompetitionDtoList.forEach(academyCompetitionDto->this.addAcademyComp(stageId,academyCompetitionDto));
        return academyCompetitionDtoList.size();
    }

    /**
     * 删除一条学院比赛信息
     */
    @Transactional(rollbackFor = Exception.class, propagation= Propagation.REQUIRED)
    public int delAcademyComp(int stageId, int academyId){
        if(academyCompetitionMapper.selectCount(new QueryWrapper<AcademyCompetition>().eq("stage_id",stageId).eq("academy_id",academyId))<1){
            throw new AppException(ErrorCode.ACADEMY_COMPETITION_LACK);
        }
        return academyCompetitionMapper.delete(new QueryWrapper<AcademyCompetition>().eq("stage_id",stageId).eq("academy_id",academyId));
    }

    /**
     * 更新学院剩余推荐数目
     */
    @Transactional(rollbackFor = Exception.class, propagation= Propagation.REQUIRED)
    public int updateRecAmt(int stageId, int academyId, AcademyCompetitionDto academyCompetitionDto){
        if(academyCompetitionMapper.selectCount(new QueryWrapper<AcademyCompetition>().eq("stage_id",stageId).eq("academy_id",academyId))<1){
            throw new AppException(ErrorCode.ACADEMY_COMPETITION_LACK);
        }
        AcademyCompetition academyCompetition = new AcademyCompetition();
        BeanUtils.copyProperties(academyCompetitionDto, academyCompetition);
        academyCompetition.setStageId(stageId);
        academyCompetition.setAcademyId(academyId);
        return academyCompetitionMapper.updateById(academyCompetition);
    }

    /**
     * 更新学院竞赛信息
     */
    @Transactional(rollbackFor = Exception.class, propagation= Propagation.REQUIRED)
    public int updateAcademyCompetition(AcademyCompetition academyCompetition){
        if(academyCompetitionMapper.selectCount(new QueryWrapper<AcademyCompetition>().eq("stage_id",academyCompetition.getStageId()).eq("academy_id",academyCompetition.getAcademyId()))<1){
            throw new AppException(ErrorCode.ACADEMY_COMPETITION_LACK);
        }
        return academyCompetitionMapper.update(academyCompetition, new UpdateWrapper<AcademyCompetition>().eq("stage_id",academyCompetition.getStageId()).eq("academy_id",academyCompetition.getAcademyId()));
    }

    /**
     * 根据学院Id和竞赛Id获取信息
     *
     * @param academyId
     * @param stageId
     * @return
     */
    public AcademyCompetition getAcademyCompetitionById(int academyId, int stageId){
        if(academyCompetitionMapper.selectCount(new QueryWrapper<AcademyCompetition>().eq("stage_id",stageId).eq("academy_id",academyId))<1){
            throw new AppException(ErrorCode.ACADEMY_COMPETITION_LACK);
        }
        return academyCompetitionMapper.selectOne(new QueryWrapper<AcademyCompetition>().eq("academy_id",academyId).eq("stage_id",stageId));
    }

    /**
     * 根据学院Id和竞赛Id获取Vo信息
     *
     * @param academyId
     * @param stageId
     * @return
     */
    public AcademyCompetitionVo getAcademyCompetitionVoById(int academyId, int stageId){
        if(academyCompetitionMapper.selectCount(new QueryWrapper<AcademyCompetition>().eq("stage_id",stageId).eq("academy_id",academyId))<1){
            throw new AppException(ErrorCode.ACADEMY_COMPETITION_LACK);
        }
        AcademyCompetition academyCompetition = academyCompetitionMapper.selectOne(new QueryWrapper<AcademyCompetition>().eq("academy_id", academyId).eq("stage_id", stageId));
        AcademyCompetitionVo academyCompetitionVo = new AcademyCompetitionVo();
        BeanUtils.copyProperties(academyCompetition, academyCompetitionVo);
        return academyCompetitionVo;
    }


    /**
     * 校级管理员-根据竞赛Id获取直推省级信息
     *
     * @param stageId
     * @return
     */
    public DirectProvinceInfoVo getDirectProvinceInfoVo(int stageId){
        DirectProvinceInfoVo directProvinceInfoVo = new DirectProvinceInfoVo();
        Integer directProvinceRecNum =  academyCompetitionMapper.getDirectProvinceRecNum(stageId);
        directProvinceInfoVo.setCompSchoolRecAmt(directProvinceRecNum == null ? 0 : directProvinceRecNum);
        Integer directProvinceAlreadyRecNum = projectService.getDirectProvinceAlreadyRecNum(stageId);
        directProvinceInfoVo.setAlreadyRecNum(directProvinceAlreadyRecNum == null ? 0 : directProvinceAlreadyRecNum);
        directProvinceInfoVo.setEnrollNum(projectService.getProvinceRecEnrollNum(stageId));
        return directProvinceInfoVo;
    }

    /**
     * 校级管理员-根据竞赛Id获取学院信息
     *
     * @param compId
     * @return
     */
    public List<AcademyCompetitionRoughInfoVo> getAcademyCompetitionRoughInfoVoList(int compId, int stageId){
        if(academyCompetitionMapper.selectCount(new QueryWrapper<AcademyCompetition>().eq("stage_id",stageId))<1){
            throw new AppException(ErrorCode.ACADEMY_COMPETITION_LACK);
        }
        List<AcademyCompetitionRoughInfoVo> academyCompetitionRoughInfoVoList = new ArrayList<>();
        List<AcademyCompetition> academyCompetitionByStageId = academyCompetitionMapper.selectList(new QueryWrapper<AcademyCompetition>().eq("stage_id",stageId));
        for (AcademyCompetition academyCompetition : academyCompetitionByStageId) {
            AcademyCompetitionRoughInfoVo academyCompetitionRoughInfoVo = new AcademyCompetitionRoughInfoVo();
            BeanUtils.copyProperties(academyCompetition, academyCompetitionRoughInfoVo);
            //院级管理员已提交项目则校级管理员视角院级已审核项目为除去自评三等奖、校团委、省级推荐之外拟推荐的普通项目,否则为0
            if(StatusConstant.PROJECT_ALREADY_SUBMIT.getCode().equals(academyCompetition.getSubmitStatus() ? 1 : 0)){
                academyCompetitionRoughInfoVo.setAcademyAlreadyRecNum(projectService.getAcademyAlreadyRecNum(stageId, academyCompetition.getAcademyId()));
            }else{
                academyCompetitionRoughInfoVo.setAcademyAlreadyRecNum(0);
            }
            academyCompetitionRoughInfoVo.setSubmitStatus(academyCompetition.getSubmitStatus());
            academyCompetitionRoughInfoVo.setEnrollNum(projectService.getSchoolNormalProjectsEnroll(stageId, academyCompetition.getAcademyId()));
            academyCompetitionRoughInfoVo.setSchoolRecPassNum(projectService.getSchoolRecPassNum(stageId, academyCompetition.getAcademyId()));
            academyCompetitionRoughInfoVoList.add(academyCompetitionRoughInfoVo);
        }
        return academyCompetitionRoughInfoVoList;
    }


}
